﻿using System.Collections.Generic;
using QFramework;
using UnityEngine;
using System;

namespace HareGame
{
    [MonoSingletonPath("GameEntry/Localization")]
    public class LocalizationManager : MonoBehaviour, ISingleton
    {
        public SystemLanguage curLanguage = SystemLanguage.Chinese;
        public List<LocalizationData> m_Language;

        /// <summary>
        /// 初始化
        /// </summary>
        public void OnSingletonInit()
        {
            m_Language = GameEntry.Util.ReadJsonDataFromStreamAssets<LocalizationData>("Localization");
            curLanguage = GameEntry.GameData.GetObject(UserData.Language, Application.systemLanguage);
        }

        /// <summary>
        /// 定义单例
        /// </summary>
        public static LocalizationManager Instance
        {
            get { return MonoSingletonProperty<LocalizationManager>.Instance; }
        }

        /// <summary>
        /// 改变游戏语言
        /// </summary>
        /// <param name="language">语言编码</param>
        public void ChangeLanguage(SystemLanguage language)
        {
            curLanguage = language;
            GameEntry.GameData.SetObject(UserData.Language, curLanguage);
            GameEntry.Event.Send(EventID.LanguageChanged);
        }

        /// <summary>
        /// 获取多语言文本
        /// </summary>
        /// <param name="key">多语言key</param>
        /// <param name="language">语言</param>
        /// <returns></returns>
        public string GetValue(string key, SystemLanguage language = SystemLanguage.Unknown)
        {
            if (string.IsNullOrEmpty(key))
                return "";

            if (language == SystemLanguage.Unknown)
                language = curLanguage;
            string value = key;
            string fallback = "";
            bool hasKey = false;
            foreach (var v in m_Language)
            {
                if (v.Key == key)
                {
                    hasKey = true;
                    fallback = v.English;
                    switch (language)
                    {
                        case SystemLanguage.English:
                            value = v.English;
                            break;
                        case SystemLanguage.Chinese:
                        case SystemLanguage.ChineseSimplified:
                            value = v.Chinese;
                            break;
                        case SystemLanguage.ChineseTraditional:
                            value = v.ChineseTraditional;
                            break;
                        case SystemLanguage.German:
                            value = v.German;
                            break;
                        case SystemLanguage.Spanish:
                            value = v.Spanish;
                            break;
                        case SystemLanguage.French:
                            value = v.French;
                            break;
                        case SystemLanguage.Indonesian:
                            value = v.Indonesian;
                            break;
                        case SystemLanguage.Italian:
                            value = v.Italian;
                            break;
                        case SystemLanguage.Japanese:
                            value = v.Japanese;
                            break;
                        case SystemLanguage.Korean:
                            value = v.Korean;
                            break;
                        case SystemLanguage.Portuguese:
                            value = v.Portuguese;
                            break;
                        case SystemLanguage.Russian:
                            value = v.Russian;
                            break;
                        case SystemLanguage.Thai:
                            value = v.Thai;
                            break;
                        case SystemLanguage.Turkish:
                            value = v.Turkish;
                            break;
                        case SystemLanguage.Vietnamese:
                            value = v.Vietnamese;
                            break;
                        default:
                            value = v.English;
                            break;
                    }
                }
            }
            if (!hasKey)
                LogKit.E("不存在Key为<color=red>" + key + "</color>的多语言字段");

            if (string.IsNullOrEmpty(value))
                return fallback;

            return value;
        }
    }

    [Serializable]
    public struct LocalizationData
    {
        public string Key;
        public string English;
        public string Chinese;
        public string ChineseTraditional;
        public string German;
        public string Spanish;
        public string French;
        public string Indonesian;
        public string Italian;
        public string Japanese;
        public string Korean;
        public string Portuguese;
        public string Russian;
        public string Thai;
        public string Turkish;
        public string Vietnamese;
    }
}